package com.nebula.salary.model.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.nebula.salary.model.deserializer.CustomAuthorityDeserializer;
import com.nebula.salary.model.pojo.Role;
import com.nebula.salary.model.pojo.User;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 登录用户信息
 *
 * @author codex
 * @since 2022-02-01
 */
@ApiModel(value = "登录用户信息", description = "")
@SuppressWarnings("all")
public class LoginUser implements Serializable, UserDetails {
    private static final long serialVersionUID = 1L;
    
    @ApiModelProperty(value = "用户唯一标识")
    private String uuid;
    
    @ApiModelProperty(value = "登录时间")
    private Long loginTime;
    
    @ApiModelProperty(value = "登录失效时间")
    private Long expireTime;
    
    @ApiModelProperty(value = "用户信息")
    private User user;
    
    public LoginUser() {
    }
    
    public LoginUser(User user) {
        this.user = user;
    }
    
    public String getUuid() {
        return uuid;
    }
    
    public void setUuid(String uuid) {
        this.uuid = uuid;
    }
    
    public Long getLoginTime() {
        return loginTime;
    }
    
    public void setLoginTime(Long loginTime) {
        this.loginTime = loginTime;
    }
    
    public Long getExpireTime() {
        return expireTime;
    }
    
    public void setExpireTime(Long expireTime) {
        this.expireTime = expireTime;
    }
    
    public User getUser() {
        return user;
    }
    
    public void setUser(User user) {
        this.user = user;
    }
    
    @Override
    @JsonDeserialize(using = CustomAuthorityDeserializer.class)
    @JsonIgnore
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return null;
        /*List<SimpleGrantedAuthority> authorities =
                user.getRoles().stream().map(role -> new SimpleGrantedAuthority(role.getRoleSecurity())).collect(Collectors.toList());
        
        return authorities;*/
    }
    
    @Override
    @JsonIgnore
    public String getPassword() {
        return user.getPassword();
    }
    
    @Override
    @JsonIgnore
    public String getUsername() {
        return user.getUsername();
    }
    
    @Override
    @JsonIgnore
    public boolean isAccountNonExpired() {
        return true;
    }
    
    @Override
    @JsonIgnore
    public boolean isAccountNonLocked() {
        return true;
    }
    
    @Override
    @JsonIgnore
    public boolean isCredentialsNonExpired() {
        return true;
    }
    
    @Override
    @JsonIgnore
    public boolean isEnabled() {
        return true;
    }
    
    @Override
    public String toString() {
        return "LoginUser{" +
                "uuid='" + uuid + '\'' +
                ", loginTime=" + loginTime +
                ", expireTime=" + expireTime +
                ", user=" + user +
                '}';
    }
}
